package cn.flying.system.web.cm;

import javax.annotation.Resource;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.flying.base.common.vo.PageRequest;
import cn.flying.base.common.vo.PageResponse;
import cn.flying.base.common.vo.Rt;
import cn.flying.base.common.vo.XmSelect;
import cn.flying.system.domain.cm.CmRole;
import cn.flying.system.domain.cm.CmRoleResource;
import cn.flying.system.service.cm.CmRoleService;

/**
 * @description: 角色请求处理
 * @author: lvyong
 * @date: 2022年06月04日 21:19
 * @version: 1.0
 */
@Controller
@RequestMapping("role")
public class CmRoleController {

    @Resource
    private CmRoleService cmRoleService;

    /**
     * 跳转到列表页面
     *
     * @return
     */
    @RequestMapping("list")
    public String list() {
        return "sys/role/list";
    }

    /**
     * 加载列表数据
     *
     * @return
     */
    @RequestMapping(value = "getRolePageList", method = RequestMethod.POST)
    @ResponseBody
    public PageResponse<CmRole> getRolePageList(PageRequest<CmRole> pageRequest) {
        PageResponse<CmRole> pageResponse = cmRoleService.getRolePageList(pageRequest);
        return pageResponse;
    }

    /**
     * 新增/修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/turn/edit")
    public String turnEdit(Long id, Model model) {
        if (null != id) {
            CmRole cmRole = cmRoleService.getRoleById(id);
            model.addAttribute("role", cmRole);
        } else {
            model.addAttribute("role", new CmRole());
        }
        return "sys/role/edit";
    }

    /**
     * 保存
     *
     * @param cmRole
     * @return
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public Rt save(@RequestBody CmRole cmRole) {
        if (cmRole.getId() == null) {
            cmRoleService.insertRole(cmRole);
        } else {
            cmRoleService.updateRoleById(cmRole);
        }
        return Rt.success("保存成功！");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "del/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Rt del(@PathVariable("id") Long id) {
        cmRoleService.deleteRoleById(id);
        return Rt.success("删除成功！");
    }

    /**
     * 权限分配页面
     *
     * @param roleId
     * @param model
     * @return
     */
    @RequestMapping("/turn/perms")
    public String turnPermission(String roleId, Model model) {
        model.addAttribute("roleId", roleId);
        List<CmRoleResource> resources = cmRoleService.getRoleResourceList(roleId);
        model.addAttribute("resources", resources);
        return "sys/role/resource";
    }

    /**
     * 保存角色资源关联
     *
     * @param resources
     * @return
     */
    @PostMapping(value = "/perms/save")
    @ResponseBody
    public Rt permissionSave(@RequestBody List<CmRoleResource> resources) {
        cmRoleService.insertRoleResources(resources);
        return Rt.success("保存成功！");
    }

    /**
     * 获取角色下拉框数据 xm-select
     *
     * @param
     * @return
     */
    @PostMapping(value = "getRoleSelectList")
    @ResponseBody
    public Rt<List<XmSelect>> getRoleSelectList() {
        List<XmSelect> list = cmRoleService.getRoleList();
        return Rt.success(list);
    }

}

